package process;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import dao_manager.UserInfoManager;
import dao_manager.UserInfoManagerImpl;
import beans.ClientInfo;
import form.NewClientForm;

public class ProcessNewClient extends Action {

    private static final String ATT_USER_ID = "userID";
    Logger                      l           = LoggerFactory.getLogger( ProcessNewClient.class );
    long user_id;

    public ProcessNewClient()
    {
        super();
    }

    @SuppressWarnings( "deprecation" )
    public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response )
            throws Exception {

        ActionErrors errors = new ActionErrors();
        ClientInfo clientInfo=null;
        l.debug( "starting new client Form " );
        
        
        // saving client in --------------------------------------------------
        // database---------------------------------------------
       try{
            NewClientForm clientForm = (NewClientForm) form;
            clientInfo = new ClientInfo( clientForm);
            user_id = (long) request.getSession().getAttribute( ATT_USER_ID );
            
            UserInfoManager userInfoManager = new UserInfoManagerImpl();
            userInfoManager.addClient( clientInfo, user_id, request );
       }
       catch( Exception e)
       {
           errors.add( "erreur creation client", new ActionMessage( "errors.erreurClientCreation" ) );
       }

        // retour du mapping ou reaffichage des erreurs ---------------
        // ---------------------------------------------------------------------------------------------
        if ( !errors.isEmpty() )
        {
            saveErrors( request, errors );
            return ( new ActionForward( mapping.getInput() ) );
        }
        else
            return mapping.findForward( "success" );
    }
}
